﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DAOChiTietPhieuNhap:DataProvider
    {
        public static DataTable getAll(int maphieunhap)
        {
            string select = "select * from chitietphieunhap where maphieunhap=@maphieunhap";
            var da = new SqlDataAdapter(select, KetNoi());
            da.SelectCommand.Parameters.AddWithValue("@maphieunhap", maphieunhap);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DongKetNoi();
            return dt;
        }

        public static int add(DTOChiTietPhieuNhap obj)
        {
            string select = "select * from chitietphieunhap";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            var row = dt.NewRow();
            row["MaNVL"] = obj.MaNVL;
            row["MaPhieuNhap"] = obj.MaPhieuNhap;
            row["SoLuongNhap"] = obj.SoLuongNhap;
            row["GiaTien"] = obj.GiaTien;
            row["TongTien"] = obj.SoLuongNhap * obj.GiaTien;
            dt.Rows.Add(row);
            DongKetNoi();
            return da.Update(dt);
        }

        public static int edit(DTOChiTietPhieuNhap obj)
        {
            string select = "select * from chitietphieunhap";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            da.Fill(dt);

            var row = dt.Rows.Find(obj.id);
            row.BeginEdit();
            row["MaNVL"] = obj.MaNVL;
            row["MaPhieuNhap"] = obj.MaPhieuNhap;
            row["SoLuongNhap"] = obj.SoLuongNhap;
            row["GiaTien"] = obj.GiaTien;
            row["TongTien"] = obj.SoLuongNhap * obj.GiaTien;
            row.EndEdit();

            DongKetNoi();
            return da.Update(dt);
        }

        public static int del(DTOChiTietPhieuNhap obj)
        {
            string select = "select * from chitietphieunhap";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            da.Fill(dt);

            var row = dt.Rows.Find(obj.id);
            row.Delete();

            DongKetNoi();
            return da.Update(dt);
        }
    }
}
